/**
 * @description 菜单JS
 * @author 赵子靖
 * @since 2017-11-22
 */
var Menu = function() {
    var t = {
        path :'',
        init : function() {
            t.initView();
            t.initEvent();
        },
        initView : function() {
            
        },
        initEvent : function() {
            
        },
        //加载数据
        render:function(){
            $("#menu-table").bootstrapTable({
                queryParams : function(params) {
                    $.extend(params, {
                    })
                    return params;
                }

            });
        },
        //添加
        addMenuDialog:function(parentId,parentName){
            $("#dialog").load(t.path+"/sysMenu/forwardAddMenu.do",function(responseTxt,statusTxt,xhr){
                if(statusTxt=="success"){
                    $('#add-menu-dialog').modal();
                    $("#add-menu-dialog #mParentId").val(parentId);
                    $("#add-menu-dialog #mParentName").val(parentName);
                }else if(statusTxt=="error"){
                    layer.alert("添加失败", {
                        closeBtn: 0,
                        shift: 2 //动画类型
                    });
                }
            });
        },
        //保存
        addMenu:function(){
            var mParentId = $("#add-menu-dialog #mParentId").val();
            var mName = $("#add-menu-dialog #mName").val();
            var mUrl = $("#add-menu-dialog #mUrl").val();
            var mStatus = $("input[name='mStatus']:checked").val();
            var mSort = $("#add-menu-dialog #mSort").val();
            var mIcon = $("#add-menu-dialog #mIcon").val();
            var mPrivilegeCode = $("#add-menu-dialog #mPrivilegeCode").val();
            if(Util.isEmpty(mName)){
                $("#mNameSpan").html("请输入菜单名称。");
                $("#add-menu-dialog #mName").focus();
                return;
            }
            $("#mNameSpan").html("");
            
            $.ajax({
                type:"POST",
                url:t.path+"/sysMenu/addMenu.do",
                dataType:"json",
                data:{
                    mParentId:mParentId,
                    mName:mName,
                    mUrl:mUrl,
                    mStatus:mStatus,
                    mSort:mSort,
                    mIcon:mIcon,
                    mPrivilegeCode:mPrivilegeCode
                },
                async: false,
                cache:false,
                success:function(data){
                    if(data.success){
                        alert("保存成功!");
                        $("#add-menu-dialog").modal('toggle');
                        $("#menu-table").bootstrapTable('refresh');
                    }else{
                        alert("保存失败!");
                    }
                }
            });
            
        },
        //编辑节点
        updateMenuDialog:function(id){
            $("#dialog").load(t.path+"/sysMenu/forwardUpdateMenu.do?id="+id,function(responseTxt,statusTxt,xhr){
                if(statusTxt=="success"){
                    $('#update-menu-dialog').modal();
                }else if(statusTxt=="error"){
                    layer.alert("添加失败", {
                        closeBtn: 0,
                        shift: 2 //动画类型
                    });
                }
            });
        },
        //修改
        updateMenu:function(){
            var mId = $("#update-menu-dialog #mId").val();
            var mParentId = $("#update-menu-dialog #mParentId").val();
            var mName = $("#update-menu-dialog #mName").val();
            var mUrl = $("#update-menu-dialog #mUrl").val();
            var mStatus = $("input[name='mStatus']:checked").val();
            var mSort = $("#update-menu-dialog #mSort").val();
            var mIcon = $("#update-menu-dialog #mIcon").val();
            var mPrivilegeCode = $("#update-menu-dialog #mPrivilegeCode").val();
            if(Util.isEmpty(mName)){
                $("#mNameSpan").html("请输入菜单名称。");
                $("#update-menu-dialog #mName").focus();
                return;
            }
            $("#mNameSpan").html("");
            
            $.ajax({
                type:"POST",
                url:t.path+"/sysMenu/updateMenu.do",
                dataType:"json",
                data:{
                    mId:mId,
                    mParentId:mParentId,
                    mName:mName,
                    mUrl:mUrl,
                    mStatus:mStatus,
                    mSort:mSort,
                    mIcon:mIcon,
                    mPrivilegeCode:mPrivilegeCode
                },
                async: false,
                cache:false,
                success:function(data){
                    if(data.success){
                        alert("保存成功!");
                        $("#update-menu-dialog").modal('toggle');
                        $("#menu-table").bootstrapTable('refresh');
                    }else{
                        alert("保存失败!");
                    }
                }
            });
            
        },
        //删除节点
        deldteMenuDialog:function(id){
            bootbox.confirm({
                size : 'small',
                message : "确认删除该信息，删除后将不可恢复？",
                callback : function(result) {
                    if (result) {
                        $.ajax({
                            type:"POST",
                            url:t.path+"/sysMenu/deleteMenu.do",
                            dataType:"json",
                            data:{
                                id:id
                            },
                            async: false,
                            cache:false,
                            success:function(data){
                                if (data && data.success) {
                                    alert("删除成功。");
                                    $("#menu-table").bootstrapTable('refresh');
                                } else {
                                    alert("删除失败.");
                                }
                            }
                        });
                    }
                }
            });
        },
        statusFormatter:function(value,row){
            if(value==1){
                return '正常';
            }else{
                return "<font style='color:red;'>停用</font>";
            }
        },
        fileFormatter:function(value,row){
            if(!Util.isEmpty(value)){
                return "<a href='"+t.path+"/upload/"+value+"'>"+value+"</a>";
            }
        },
        dateFormatter:function(value,row){
            if(!Util.isEmpty(value)){
                return value.substring(0,10);
            }
        },
        urlFormatter:function(value,row){
            if(!Util.isEmpty(value)){
                return "<a href='"+value+"' target='_blank'>"+value+"</a>";
            }
        },
        interfaceNumFormatter:function(value,row){
            return ' <a href="javascript:MenuInterface.loadMenuInterfaceDialog(\''+row.hjlIsId+'\')">'+value+' </a>';
        },
        iconFormatter:function(value,row){
            return '<i class="'+value+'"></i>';
        },
        //子菜单
        detailFormatter: function(index, row) {
            var html = "<div style='width:90%; margin:auto;'>";
            html += "<table class='party-special-detail-table' border = '1'  >";
            html += "<tr>";
            html += "<td align = 'center' width = '20%'>菜单名称</td>";
            html += "<td align = 'center' width = '20%' >菜单链接</td>";
            html += "<td align = 'center' width = '20%' >权限码</td>";
            html += "<td align = 'center' width = '15%' >排序</td>";
            html += "<td align = 'center' width = '15%' >状态</td>";
            html += "<td align = 'center'  width = '15%' >操作</td>";
            html += "</tr>";
            $.ajax({
                type:"POST",
                url:t.path+"/sysMenu/getMenuByParent.do",
                dataType:"json",
                data:{
                    mParentId:row.mId
                },
                async: false,
                cache:false,
                success:function(data){
                    if(data && data.rows){
                        for (var i = 0; i < data.rows.length; i++) {
                            html += "<tr>";
                            html += "<td align = 'center'>" + data.rows[i].mName + "</td>";
                            html += "<td align = 'center'>" + data.rows[i].mUrl + "</td>";
                            html += "<td align = 'center'>" + data.rows[i].mPrivilegeCode + "</td>";
                            html += "<td align = 'center'>" + data.rows[i].mSort + "</td>";
                            if(data.rows[i].mStatus==1){
                                html += "<td align = 'center'>正常</td>";
                            }else{
                                html += "<td align = 'center'><font style='color:red;'>停用</font></td>";
                            }
                            html += '<td align = \'center\'><a href="javascript:Menu.updateMenuDialog(\'' + data.rows[i].mId + '\')" class="btn btn-default btn-xs">编辑</a>&nbsp; <a href="javascript:Menu.deldteMenuDialog(\'' + data.rows[i].mId + '\')" class="btn btn-default btn-xs">删除</a></td>';
                            html += "</tr>";
                        }
                    }else{
                        html += "<tr><td colspan='4'>暂无数据</td></tr>";
                    }
                }
            });
            
            return html;
        },
        operator : function(value, row) {
            var html = "";
            html += '<a href="javascript:Menu.addMenuDialog(\'' + value + '\',\''+row.mName+'\')" class="btn btn-default btn-xs">添加子菜单</a>&nbsp;';
            html += '<a href="javascript:Menu.updateMenuDialog(\'' + value + '\')" class="btn btn-default btn-xs">编辑</a>&nbsp;';
            html += '<a href="javascript:Menu.deldteMenuDialog(\'' + value + '\')" class="btn btn-default btn-xs">删除</a>';
            return html;
        }
    }
    return t;
}();